package com.ph.shopping.facade.member.service.impl;


import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.member.dto.CheckMemberAuthDto;
import com.ph.shopping.facade.member.dto.MemberAuthDto;
import com.ph.shopping.facade.member.dto.QueryMemberAuthDto;
import com.ph.shopping.facade.member.entity.MemberAuth;
import com.ph.shopping.facade.member.service.IMemberAuthService;
import com.ph.shopping.facade.member.service.dbservice.facade.IMemberAuthDbService;

/**
 * @项目：phshopping-service-member
 * @描述：
 * @作者： Mr.zheng
 * @创建时间：2017-03-16
 * @Copyright @2017 by Mr.zheng
 */
@Component
@Service(version = "1.0.0")
public class MemberAuthService extends BaseService implements IMemberAuthService {

    private static Logger logger = LoggerFactory.getLogger(MemberAuthService.class);

    @Autowired
    private IMemberAuthDbService memberAuthDbService;
    
	/**
     * 推广师申请
     * @param memberAuthDto
     * @return Result
     */
    @Override
    public Result apply(MemberAuthDto memberAuthDto) {
        logger.info("推广师申请入参,memberAuthDto={}", JSON.toJSONString(memberAuthDto));
        Result result = DubboResult.getDefResult();
        try {
            String[] fields = {"account","accountType","image","memberID"};
            if(ParamVerifyUtil.entityIsNotNullByField(memberAuthDto, fields)){
                result = memberAuthDbService.apply(memberAuthDto);
            }
        } catch (Exception e) {
            result = DubboResult.getResultByError(result);
            logger.error("推广师申请错误,ex={}",e);
        }
        logger.info("推广师申请返回值,result={}",JSON.toJSONString(result));
        return result;
    }

    /**
     * 审核推广师
     * @param checkMemberAuthDto
     * @return
     */
    @Override
    public Result check(CheckMemberAuthDto checkMemberAuthDto) {
        logger.info("审核推广师入参,checkMemberAuthDto={}", JSON.toJSONString(checkMemberAuthDto));
        Result result = DubboResult.getDefResult();
        try {
            String[] fields = {"memberID","status"};
            if(ParamVerifyUtil.entityIsNotNullByField(checkMemberAuthDto, fields)){
                result = memberAuthDbService.check(checkMemberAuthDto);
            }
        } catch (Exception e) {
            result = DubboResult.getResultByError(result);
            logger.error("审核推广师错误,ex={}",e);
        }
        logger.info("审核推广师返回值,result={}",JSON.toJSONString(result));
        return result;
    }

    /**
     * 推广师列表查询
     * @param queryMemberAuthDto
     * @return
     */
	@Override
	public Result getMemberAuthListByPage(PageBean pageBean,QueryMemberAuthDto queryMemberAuthDto) {
		logger.info("根据分页数据获取推广师列表数据入参 ,queryMemberAuthDto={} ",JSON.toJSONString(queryMemberAuthDto));
		Result result = DubboResult.getDefResult();
		try {
			if (null != pageBean) {
				result = memberAuthDbService.getMemberAuthListByPage(pageBean,queryMemberAuthDto);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			logger.error("根据分页参数查询推广师信息错误", e);
		}
		logger.info("根据分页数据获取推广师列表数据返回值,result={}", JSON.toJSONString(result));
		return result;
	}

	/**
     * 删除推广师
     * @param memberId
     * @return
     */
	@Override
	public Result deleteMemAuthByMemId(Long memberId) {
		logger.info("删除推广师列表数据入参 ,memberId={} ",memberId);
		Result result = DubboResult.getDefResult();
		try {
			if (null != memberId) {
				MemberAuth memberAuth = new MemberAuth();
				memberAuth.setMemberID(memberId);
				memberAuth.setUpdateTime(new Date());
				memberAuth.setIsDelete(Byte.valueOf("1"));
				result = memberAuthDbService.deleteMemAuthByMemId(memberAuth);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			logger.error("删除推广师列表数据错误", e);
		}
		logger.info("删除推广师列表数据返回值,result={}", JSON.toJSONString(result));
		return result;
	}

	 /**
     * 
    * @Title: getMemberAuthList  
    * @Description: 获取推广师列集合 
    * @param dto
    * @return Result    返回类型  
    * @throws
     */
	@Override
	public Result getMemberAuthList(QueryMemberAuthDto dto) {
		logger.info("获取推广师列表数据入参 ,dto={} ",JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			result = memberAuthDbService.getMemberAuthList(dto);
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			logger.error("查询推广师信息错误", e);
		}
		logger.info("获取推广师列表数据返回值,result={}", JSON.toJSONString(result));
		return result;
	}

	@Override
	public Result getMemberAuthInfo(Long memberId) {
		logger.info("获取推广师认证信息 ,memberId " + memberId);
		Result result = DubboResult.getDefResult();
		try {
			result = memberAuthDbService.queryMemberAuthInfo(memberId);
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			logger.error("获取推广师认证信息错误", e);
		}
		return result;
	}
	

}
